package com.langyiquan.front.controller;

import com.langyiquan.pojo.Carousel;
import com.langyiquan.pojo.ItemCategory;
import com.langyiquan.service.CarouselService;
import com.langyiquan.service.CategoryService;
import com.langyiquan.service.ItemService;
import com.langyiquan.utils.PagedGridResult;
import com.langyiquan.utils.RedisOperator;
import com.langyiquan.utils.UnifyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "首页", tags = {"首页展示的相关接口"})
@RestController
@RequestMapping("index")
public class IndexController extends BaseController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ItemService itemService;

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "获取轮播图",notes = "获取轮播图",httpMethod = "GET")
    @GetMapping("/carousel")
    public UnifyResponse carousel(){
        String list = redisOperator.get("carousel");
        List<Carousel> result = new ArrayList<>();
    //    if(list == null){
    //        result = carouselService.queryAll(null,null);
    //        redisOperator.set("carousel", JsonUtils.objectToJson(result));
    //    }else{
    //        result = JsonUtils.jsonToList(list,Carousel.class);
    //    }
        result = carouselService.queryAll(null,null);

        return UnifyResponse.success(result);
    }

    @ApiOperation(value = "获取商品热门分类",notes = "获取商品热门分类",httpMethod = "GET")
    @GetMapping("/hot-catgorys")
    public UnifyResponse hotCatgorys(){
        List<ItemCategory> result = categoryService.queryHotCat();
        return UnifyResponse.success(result);
    }

    @ApiOperation(value = "获取每日推荐",notes = "获取每日推荐",httpMethod = "GET")
    @GetMapping("/recommends")
    public UnifyResponse recommends(@RequestParam Integer page, @RequestParam Integer pageSize){
        PagedGridResult pagedGridResult = itemService.getRecommendItems(page,pageSize);
        return UnifyResponse.success(pagedGridResult);
    }

}
